package com.map;

import com.set.FileOperation;

import java.util.ArrayList;

/**
 * 性能测试：对比基于链表和二分搜索树的映射的性能
 */
public class PerformanceTest {
    private static double testMapPerformance(Map<String, Integer> map, String fileName) {
        long startTime = System.nanoTime();
        System.out.println(fileName);
        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile(fileName, words)) {
            System.out.println("Total words: " + words.size());
            for (String word : words) {
                if (map.contains(word)) {
                    map.add(word, map.get(word) + 1);
                }else {
                    map.add(word, 1);
                }
            }
            System.out.println("Total different words: " + map.getSize());
            System.out.println("Frequency of pride: " + map.get("pride"));
        }
        long endTime = System.nanoTime();
        return (endTime - startTime)/1000000000.0;
    }
    public static void main(String[] args) {
        String fileName =  "E:\\WorkSpace\\LearningDemoWorkSpace\\data-structure\\src\\pride-and-prejudice.txt";
        BSTMap<String, Integer> bstMap = new BSTMap<>();
        double time1 = testMapPerformance(bstMap, fileName);
        System.out.println("BST map: " + time1 + " s");

        System.out.println();
        LinkedListMap<String, Integer> linkedListMap = new LinkedListMap<>();
        double time2 = testMapPerformance(linkedListMap, fileName);
        System.out.println("LinkedListMap map: " + time2 + " s");

        System.out.println();
        AVLMap<String, Integer> avlMap = new AVLMap<>();
        double time3 = testMapPerformance(avlMap, fileName);
        System.out.println("AVLMap map: " + time3 + " s");

    }
}
